﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace HDJ.Framework.Core.Editor
{
    public class GridEditorGUI
    {
        public  CallBack<int, int, Rect> OnDrawGridItem;

        private GridData gridData =null;

        public Rect showRect;
        /// <summary>
        /// 表格内容绘制区域
        /// </summary>
        private Rect contentGridRect;
        public GridEditorGUI( GridData gridData)
        {
            this.gridData = gridData;
        }
        private int minItem = 30;
        private Vector2 svPos =Vector2.zero;

        /// <summary>
        /// 可显示的所有行
        /// </summary>
        private List<int> currentShowAllRow = new List<int>();
        public bool ShowSearchRow = false;
        private void DrawAllGridItem(Rect gridRect)
        {
           
            float totleHigh = 0;
            for (int y = 0; y < currentShowAllRow.Count; y++)
            {
                int rowIndex = currentShowAllRow[y];

                int withCount = gridData.withItemList.Count;
               
                float totleWith = 0;
                for (int x = 0; x < withCount; x++)
                {
                    //object gridItem = rowDatas[x];
                    Rect itemRect = new Rect(gridRect.x + totleWith, gridRect.y + totleHigh, gridData.withItemList[x], gridData.heightItemList[rowIndex]);
                    //DrawGridItem(itemRect,x,y);
                    if (OnDrawGridItem != null)
                    {
                        OnDrawGridItem(x, y, itemRect);
                    }
                    totleWith += gridData.withItemList[x];
                }
                totleHigh += gridData.heightItemList[rowIndex];
            }
        }
        private void CreateRowData()
        {
            currentShowAllRow.Clear();
            bool isShowAll = true;
            if (gridData.fixedShowAllRow.Count > 0)
            {
                currentShowAllRow.AddRange(gridData.fixedShowAllRow);

            }
            if (ShowSearchRow)
            {
                foreach (var item in gridData.searchShowAllRow)
                {
                    if (!currentShowAllRow.Contains(item))
                    {
                        currentShowAllRow.Add(item);
                    }
                }
                isShowAll = false;
            }
            if (isShowAll)
            {
                foreach (var item in gridData.currentShowAllRow)
                {
                    if (!currentShowAllRow.Contains(item))
                    {
                        currentShowAllRow.Add(item);
                    }
                }
            }
            currentShowAllRow.Sort();
        }
        public Vector2 GetShowContentGUISize()
        {
            float tempWith = 0;
            for (int i = 0; i < gridData.withItemList.Count; i++)
            {
                tempWith += gridData.withItemList[i];
            }
            // tempWith += ;

            float tempHeight = 0;
            foreach (var index in currentShowAllRow)
            {
                if (index < gridData.heightItemList.Count)
                {
                    tempHeight += gridData.heightItemList[index];
                }
                else
                {
                    Debug.LogError("currentShowRow 越界：" + index);
                }
            }
            // tempHeight += wHeight;
            return new Vector2(tempWith, tempHeight);
        }
        Vector2 leftGridSVPos;
        public void OnGUI()
        {
            CreateRowData();

            Vector2 size = GetShowContentGUISize();
            GUI.Box(new Rect(showRect.x, showRect.y, gridData.defaltGridItemWith, gridData.defaltGridItemHeight), "", "SelectionRect");

            contentGridRect = new Rect(showRect.x + gridData.defaltGridItemWith, showRect.y + gridData.defaltGridItemHeight, size.x,size.y);
            //GUI.color = Color.green;
            //GUI.Box(contentGridRect, "");
            //GUI.color = Color.white;
            Rect contentGridViewRect = new Rect(contentGridRect.position, new Vector2(showRect.width - gridData.defaltGridItemWith, showRect.height - gridData.defaltGridItemHeight));
            svPos = GUI.BeginScrollView(contentGridViewRect, svPos, contentGridRect);
            //数据中两排后的数据
            DrawAllGridItem(contentGridRect);

            GUI.EndScrollView();

            Debug.unityLogger.logEnabled = false;
            //顶部格子
            Vector2 topGridSVPos = new Vector2(svPos.x, 0);
            topGridSVPos = GUI.BeginScrollView(new Rect(new Vector2(showRect.x + gridData.defaltGridItemWith, showRect.y), new Vector2(contentGridViewRect.width, gridData.defaltGridItemHeight)), topGridSVPos, new Rect(new Vector2( showRect.x+gridData.defaltGridItemWith,showRect.y), new Vector2(contentGridRect.width,gridData.defaltGridItemHeight)), false, false, "Label", "Label");
            
            Debug.unityLogger.logEnabled = true;

           
          
            float tempWith = gridData.defaltGridItemWith;
            for (int i = 0; i < gridData.withItemList.Count; i++)
            {
                Rect dragR = new Rect(tempWith, showRect.y, gridData.withItemList[i], gridData.defaltGridItemHeight);
                Rect maxR = new Rect(tempWith, showRect.y, Screen.width * 5, gridData.defaltGridItemHeight);
                dragR = EditorDrawGUIUtility.DrawCanDragArea(dragR, maxR, null, EditorCanDragAreaSide.Right);
                if (dragR.width < minItem)
                    dragR.width = minItem;
                gridData.withItemList[i] = dragR.width;

                tempWith += gridData.withItemList[i];

                if (gridData.selectColumnIndexs.Contains(i))
                    GUI.color = Color.magenta;
                if (GUI.Button(new Rect(dragR.position, new Vector2(dragR.width, dragR.height)), i.ToString()))
                {
                    if (gridData.selectColumnIndexs.Contains(i))
                        gridData.selectColumnIndexs.Remove(i);
                    else
                        gridData.selectColumnIndexs.Add(i);
                }
                GUI.color = Color.white;
            }

            GUI.EndScrollView();

          
            //默认第一列
            Debug.unityLogger.logEnabled = false;
            leftGridSVPos = new Vector2(0, svPos.y);
          //  Rect vertical
            leftGridSVPos = GUI.BeginScrollView(new Rect(showRect.x, showRect.y+gridData.defaltGridItemHeight, gridData.defaltGridItemWith, contentGridViewRect.height), leftGridSVPos, new Rect(showRect.x, showRect.y , gridData.defaltGridItemWith, contentGridRect.height), false, false, "Label", "Label");
          
            Debug.unityLogger.logEnabled = true;
            
            //左边格子
            float tempHeight = showRect.y ;
          
            //GUI.color = Color.red;
            //GUI.Box(new Rect(showRect.x, showRect.y, gridData.defaltGridItemWith, gridData.defaltGridItemHeight), "");
            //GUI.color = Color.white;
            for (int i = 0; i < gridData.heightItemList.Count; i++)
            {
                Rect dragR = new Rect(showRect.x, tempHeight, gridData.defaltGridItemWith, gridData.heightItemList[i]);
              


                Rect maxR = new Rect(showRect.x, tempHeight, gridData.defaltGridItemWith, showRect.height);
                dragR = EditorDrawGUIUtility.DrawCanDragArea(dragR, maxR, null, EditorCanDragAreaSide.Bottom);
                if (dragR.height < minItem)
                    dragR.height = minItem;
                gridData.heightItemList[i] = dragR.height;

                tempHeight += gridData.heightItemList[i];

                if (gridData.selectRowIndexs.Contains(i))
                    GUI.color = Color.cyan;

                if (GUI.Button(new Rect(dragR.position, new Vector2(dragR.width, dragR.height)), i.ToString()))
                {
                    if (gridData.selectRowIndexs.Contains(i))
                        gridData.selectRowIndexs.Remove(i);
                    else
                        gridData.selectRowIndexs.Add(i);
                }
                GUI.color = Color.white;
            }
            GUI.EndScrollView();


           
        }

        

        
    }
}
